const express = require('express')

const Router = express.Router()

const mysql = require('mysql')

const sqlHelper = require('../common/sqlHelper')

//查询
Router.get('/', (req, res) => {

    const sql = "select *from student"
    const callback = (err, result) => {
        if (err) {
            console.log("出错");
            console.log(err);
        } else {
            // console.log(result);
            res.send(result)
        }
    }
    sqlHelper(sql, callback)
})
//新增
Router.post('/', (req, res) => {
    const username = req.query.name
    const sex = req.query.sex
    const age = req.query.age

    const sql = `insert into student(username,sex,age) values('${username}','${sex}','${age}')`

    const callback = (err, result) => {
        if (err) {
            console.log("出错")
            console.log(err);
        } else {
            res.send(result)
        }
    }
    sqlHelper(sql, callback)
})
//删除
Router.delete('/', (req, res) => {

    const id = req.query.id
    const sql = `delete from student where id = ${id}`
    const callback = (err, result) => {
        if (err) {
            //查询出错
            console.log("出错")
            console.log(err);
        } else {
            // 正确
            res.send(result)
        }
    }
    sqlHelper(sql, callback)

})
//修改
Router.put('/', (req, res) => {
    res.send('修改')
})


module.exports = Router;